package cn.laixueit.controller;

import cn.laixueit.config.AliProperties;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;

@Controller
public class AliController {

    @Autowired
    AliProperties payProperties;

    @GetMapping("/a")
    public void a(HttpServletResponse httpResponse,String id){
        try {
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                    payProperties.getApp_id(), payProperties.getApp_private_key(), "json", payProperties.getCharset(),
                    payProperties.getAlipay_public_key(), "RSA2"); //获得初始化的AlipayClient
            AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
            alipayRequest.setBizContent("{" +
                    " \"out_trade_no\":\"" + id + "\"," +
                    " \"total_amount\":\"" + 180 + "\"," +
                    " \"subject\":\"" + "小米手机标题" + "\"," +
                    " \"product_code\":\"QUICK_WAP_PAY\"" +
                    " }");//填充业务参数
            String form = "";
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
            httpResponse.setContentType("text/html;charset=" + payProperties.getCharset());
            httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
            httpResponse.getWriter().flush();
            httpResponse.getWriter().close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
